package com.ymall.service.impl;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.ymall.common.ServerResponse;
import com.ymall.dao.ClockMapper;
import com.ymall.pojo.ClockRecord;
import com.ymall.service.ClockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ClockServiceImpl implements ClockService{

    @Autowired
    private ClockMapper clockMapper;

    @Override
    public ServerResponse<String> clockIn(ClockRecord clockRecord) {

        if (clockRecord == null){
            return ServerResponse.createByErrorMessage("参数为空");
        }

        Integer result = clockMapper.insert(clockRecord);

        if (result ==  1) {
            log.debug("打卡成功 ：userId{} 打卡类型 clockType{} 状态 status {} 描述 description {}" ,clockRecord.getUserId(),clockRecord.getClockType(),clockRecord.getStatus(),clockRecord.getDescription());
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("打卡失败");
    }

    @Override
    public ServerResponse<PageList<ClockRecord>> selectRecordByUserId(String userId) {
        PageBounds pageBounds = new PageBounds(1, 7);
        PageList<ClockRecord>  recordsList= clockMapper.getUserPagination(userId,pageBounds);
        if (recordsList.size()<=0){
             return ServerResponse.createByErrorMessage("没有打卡记录");
        }
        return ServerResponse.createBySuccess(recordsList);
    }
}
